package com.example.suze.libraryinformationmanagementsystem.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.suze.libraryinformationmanagementsystem.Adapter.BookListAdapter;
import com.example.suze.libraryinformationmanagementsystem.Adapter.PersonListAdapter;
import com.example.suze.libraryinformationmanagementsystem.R;
import com.example.suze.libraryinformationmanagementsystem.net.API;
import com.example.suze.libraryinformationmanagementsystem.net.JSONCallBack;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.BookDatum;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.BookRoot;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.ReturnStateEntity;
import com.person.dao.BookDao;
import com.person.dao.DBManager;
import com.person.entity.Book;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by suze on 2017/6/12.
 */

public class ResultActivity extends BaseActivity {
    private List<BookDatum> books = new ArrayList<>();
    private int i ;
    private LinearLayout linearLayout;
    private ListView listView;
    private JSONCallBack callBack;
    API api = API.getAPI(ResultActivity.this);
    private final int UPDATE_ACCOUNT = 2;


    //适配;线程
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == UPDATE_ACCOUNT) {
                int arg = msg.arg1;
                if (arg == 1) {
                    BookListAdapter bookListAdapter = new BookListAdapter(ResultActivity.this,books);
                    listView.setAdapter(bookListAdapter);
                }
                if (arg == 2) {
                    linearLayout.setVisibility(View.VISIBLE);
                }
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        linearLayout = (LinearLayout) findViewById(R.id.ll_hide);
        linearLayout.setVisibility(View.GONE);
        listView = (ListView) findViewById(R.id.result_list);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ResultActivity.this, Search.class);
                Bundle bundle = new Bundle();
                    bundle.putString("name", books.get(position).getbookname());
                    bundle.putString("author", books.get(position).getauthor());
                    bundle.putString("abstract1", books.get(position).getabstract1());
                    bundle.putString("publishing", books.get(position).getpublishing());
                if(books.get(position).getis_lend()){
                    bundle.putInt("status",1);
                }else {
                    bundle.putInt("status", 0);

                }
                bundle.putLong("borrowerId",5120145303l);
                bundle.putString("id",books.get(position).getNo());
                bundle.putString("img",books.get(position).getimg());
                    //bundle.putString("img",books.get(position).getImg());
                    intent.putExtras(bundle);
                    startActivity(intent);
            }
        });
        Bundle bundle = getIntent().getExtras();
        String search = bundle.getString("search");
        callBack = new JSONCallBack<BookRoot>(new BookRoot()) {
            @Override
            public void onSuccess(BookRoot entity) {
                if(entity.getstate().equals("success") && entity.getcode() == 0){
                    if(entity.getdata().size()>0){
                        for(int i =0;i<entity.getdata().size();i++)
                        {
                            books.add(entity.getdata().get(i));
                        }
                        Thread thread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Message message = new Message();
                                message.arg1 = 1;
                                message.what = UPDATE_ACCOUNT;
                                handler.sendMessage(message);
                            }
                        });
                        thread.start();


                    }else {
                        Thread thread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Message message = new Message();
                                message.arg1 = 2;
                                message.what = UPDATE_ACCOUNT;
                                handler.sendMessage(message);
                            }
                        });
                        thread.start();

                    }

                }else{
                    Looper.prepare();
                    Toast.makeText(ResultActivity.this, "查询失败", Toast.LENGTH_LONG).show();
                    Looper.loop();
                }
            }
        };
        api.sqlOporate("select", "SELECT * FROM book WHERE author = '"+search+"' or book_name = '"+search+"'or publishing = '"+search+"'", callBack);
//        DBManager dbManager = new DBManager(this);
//        BookDao bookdao = dbManager.getDaoSession().getBookDao();
//        books = bookdao.queryBuilder().where(BookDao.Properties.Name.like(search)).build().list();
//        author = bookdao.queryBuilder().where(BookDao.Properties.Author.like(search)).build().list();
//        publishing = bookdao.queryBuilder().where(BookDao.Properties.Publishing.like(search)).build().list();
//        if(books.size()>0){
//            for(int i = 0 ;i<books.size();i++)
//            {
//                all.add(books.get(i));
//            }
//        }
//        if(author.size()>0){
//            for(int i = 0 ;i<author.size();i++)
//            {
//                all.add(author.get(i));
//            }
//        }
//        if(publishing.size()>0){
//            for(int i = 0 ;i<publishing.size();i++)
//            {
//                all.add(publishing.get(i));
//            }
//        }
//        if(all.size()>0){
//            Log.i("----->>>>>",""+all.size());
//            BookListAdapter bookListAdapter = new BookListAdapter(this,all);
//            listView.setAdapter(bookListAdapter);
//        }else {
//            linearLayout.setVisibility(View.VISIBLE);
//        }

    }
}
